package org.columba.addressbook.parser;

import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

public class XMLContactIndexParser 
{
	private Document doc;

	protected Element parentElement;

	private Element root;
	
	List subElements;
	
	public XMLContactIndexParser() {
		doc = new Document();
		root = new Element("contactindex");
		doc.addContent(root);
	}
	
	public XMLContactIndexParser(Document document) throws SyntaxException {
		if (document == null)
			throw new IllegalArgumentException("document == null");

		this.doc = document;

		this.root = doc.getRootElement();
		
		this.subElements = root.getChildren();

		if (!root.getName().equalsIgnoreCase("contactindex")) {
			// wrong xml-format
			throw new SyntaxException("Root element must be <contactindex>!");
		}
	}
	
	public Element getRootElement() {
		return root;
	}
	
	protected Element getParentElement(int index)
	{
		return (Element)subElements.get(index);
	}
	
	public void addParentElement(Element parentElement)
	{
		root.addContent(parentElement);
		subElements.add(parentElement);
		this.parentElement = parentElement;
	}

	public void set(String key, String value) {
		Element child = parentElement.getChild(key);
		if (child == null) {
			child = new Element(key);
			parentElement.addContent(child);
		}
		child.setText(value);
	}
	
	public void set(String key, String prefix, String value) {
		Element child = parentElement.getChild(key);
		if (child == null) {
			child = new Element(key);
			parentElement.addContent(child);
		}
		Element prefixchild = child.getChild(prefix);
		if (prefixchild == null) {
			prefixchild = new Element(prefix);
			child.addContent(prefixchild);
		}
		prefixchild.setText(value);
	}

	public String get(int index, String key) {
		Element child = getParentElement(index).getChild(key);
		if (child == null) {
			child = new Element(key);
			getParentElement(index).addContent(child);
		}
		return child.getTextNormalize();
	}

	public String get(int index,String key, String prefix) 
	{
		Element child = getParentElement(index).getChild(key);
		if (child == null) {
			child = new Element(key);
			getParentElement(index).addContent(child);
		}
		Element prefixchild = child.getChild(prefix);
		if (prefixchild == null) {
			prefixchild = new Element(prefix);
			child.addContent(prefixchild);
		}

		return prefixchild.getTextNormalize();
	}

	public Document getDocument() {
		return doc;
	}
	
	public String getIndex(int index)
	{
		return get(index,"index");
	}
	
	public void setIndex(String index)
	{
		set("index", index);
	}
	
	public String getEmail(int index)
	{
		return get(index, "email");
	}
	
	public void setEmail(String email)
	{
		set("email", email);
	}
	
	public String getName(int index)
	{
		return get(index, "name");
	}
	
	public void setName(String name)
	{
		set("name", name);
	}
	public int getElementCount()
	{
		return root.getContentSize();
	}
}
